package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.LeaseOrderListEntity;
import com.sgb.kjwl.view.event.LeaseOrderEvent;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.self.LeaseOrderStateFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseOrderStateModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private LeaseOrderStateFragment fragment;
    private int page = 1;
    private int size = 10;

    public LeaseOrderStateModel(LeaseOrderStateFragment fragment) {
        this.fragment = fragment;
    }

    //租赁订单-列表筛选
    public void getLeaseOrderListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("pageNum", page);
        map.get().put("pageSize", size);
        APIManager.getInstance().getLeaseAPI().getList(map.get())
                .enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaseOrderListEntity>>>>(true) {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<LeaseOrderListEntity>>> data) {
                        if (data == null || data.getData() == null) {
                            fragment.onLoadComplete(isLoadMore);
                            return;
                        }
                        fragment.showList(data.getData().getList(), isLoadMore);
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseOrderListEntity>>>> call, Throwable t) {
                        fragment.onLoadComplete(isLoadMore);
                    }
                });
    }

    //租赁订单-删除
    public void deleteOrder(String orderNo, String type) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().deleteOrder(orderNo, type)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) return;
                        if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                            MToast.showToast(fragment.getActivity(), "删除成功");
                            EventBus.getDefault().postSticky(LeaseOrderEvent.make());//刷新列表
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                        MToast.showToast(fragment.getActivity(), "删除失败");
                    }
                });
    }


    /**
     * 租赁订单-app跳转到H5
     * @param id
     * @param type 1租入，2租出
     * @param orderType 租赁类型 1机械，1 设备
     */
    public void getH5Url(String id, String type,String orderType) {
        String name = "";
        if("1".equals(orderType)){
            if ("1".equals(type)) {
                name = "receivedOrder";//求租订单详情
            } else {
                name = "leaseOrder";//出租订单详情
            }
        }else if("2".equals(orderType)){
            if ("1".equals(type)) {
                name = "receivedOrderFacility";//求租订单详情
            } else {
                name = "equipmentRentOutOrderDetails";//出租订单详情
            }
        }
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
